package com.ydc.ln.tutorial;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Scanner;

public class FileTest {
	public static void main(String[] args) {
		FileTest test = new FileTest();
		test.writeData();
	}
	
	// 读取文件或目录信息
	public void readFileInfo() {
		File file = new File("f:\\ln_java\\m1.jpg");
		System.out.println("is exist?\t" + file.exists());
		System.out.println("can read?\t" + file.canRead());
		System.out.println("can written?\t" + file.canWrite());
		System.out.println("is directory?\t" + file.isDirectory());
		System.out.println("is file?\t" + file.isFile());
		System.out.println("is absolute?\t" + file.isAbsolute());
		System.out.println("is hidden?\t" + file.isHidden());
		System.out.println("absolute path is\t" + file.getAbsolutePath());
		System.out.println("last modified on:\t" + new Date(file.lastModified()));
	}
	
	// 用PrintWriter写数据
	public void writeData() {
		File file = new File("f:\\ln_java\\scores.txt");
		if(file.exists()) {
			file.delete();
			System.out.println("file already exists");
			System.exit(0);
		}
		PrintWriter output = null;
		try {
			output = new PrintWriter(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		output.print("John T Smith ");
		output.println(90);
		output.print("Eric K Jones ");
		output.println(85);
		
		output.close();
		
	}
	
	// 作用Scanner读数据
	public void readData() {
		File file = new File("f:\\ln_java\\scores.txt");
		
		Scanner input = null;
		try {
			input = new Scanner(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		while(input.hasNext()) {
			String firstName = input.next();
			String mi = input.next();
			String lastName = input.next();
			int score = input.nextInt();
			System.out.println(firstName + " " + mi + " " + lastName + " " + score);
		}
		
		input.close();
		
	}
}
